package sk.ogame.realm.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sk.ogame.brain.Memory;
import sk.ogame.brain.thing.CarryThing;
import sk.ogame.brain.thing.Thing;
import sk.ogame.brain.thing.ThingService;
import sk.ogame.brain.thing.WaitThing;
import sk.ogame.entity.Fleet;
import sk.ogame.entity.Planet;
import sk.ogame.entity.Position;
import sk.ogame.realm.exception.BaseException;
import sk.ogame.realm.exception.NotSupportThingException;
import sk.ogame.realm.utils.FleetsBuilder;
import sk.ogame.realm.utils.RandomUtil;

import static sk.ogame.realm.base.Const.*;

/**
 * @author 支绍昆
 * 2023/2/15
 **/
@Service
public class SailService implements ThingService {

    private static final Logger logger = LoggerFactory.getLogger(SailService.class);

    @Autowired
    private Memory memory;
    @Autowired
    private PlanetService planetService;
    @Autowired
    private ClientHttpService clientHttpService;

    @Override
    public Thing dealThing(Thing thing) {
        if (thing instanceof CarryThing) {
            CarryThing carry = (CarryThing) thing;
            carry(0, carry.getTargetId(), carry.getMetal(), carry.getCrystal(), carry.getDeuterium());
            //todo 增加等待事务.
            WaitThing waitThing = new WaitThing();
            waitThing.setTimeStamp(System.currentTimeMillis() + 1000L * 60 * 30);
            return waitThing;
        } else {
            throw new NotSupportThingException(null, 0);
        }
    }

    public void carry(int from, int target, int metal, int crystal, int deuterium) {
        if (0 == from) {
            from = memory.getMasterPlanet();
        }
        //源星是否有足够资源
        Planet src = memory.getPlanetInPeriod(from);
        if (src.getMetal() < metal || src.getCrystal() < crystal || src.getDeuterium() < deuterium) {
            throw new BaseException("not enough resources from src!", 0);
        }

        Position p = memory.getPosition(target);
        int sum = metal + crystal + deuterium;
        int shipNumber = countShipNumber(SHIP_LARGE_CARGO, sum);
        FleetsBuilder fb = new FleetsBuilder().set(SHIP_LARGE_CARGO, shipNumber);

        Fleet fleet = new Fleet().setMission(3).setTarget_type(1)
                .setTarget_galaxy(p.getGalaxy()).setTarget_system(p.getSystem()).setTarget_planet(p.getPlanet())
                .setFleets(fb.toString()).setSpeed(10).setStay_time(1)
                .setMetal(metal).setCrystal(crystal).setDeuterium(deuterium)
                .setHash(RandomUtil.charAndDigit(6));

        if (!planetService.isCurrent(from)) {
            planetService.change(from);
        }
        clientHttpService.fleetMission(fleet);
    }

    public void attack(int from, Position position){
        FleetsBuilder fb = new FleetsBuilder().set(SHIP_LARGE_CARGO, 600);
        Fleet fleet = new Fleet().setMission(1).setTarget_type(1)
                .setTarget_galaxy(position.getGalaxy()).setTarget_system(position.getSystem()).setTarget_planet(position.getPlanet())
                .setFleets(fb.toString()).setSpeed(10).setStay_time(1)
                .setMetal(0).setCrystal(0).setDeuterium(0)
                .setHash(RandomUtil.charAndDigit(6));
    }

    //-------------------------------- private ---------------------------------------

    public static int countShipNumber(int shipId, int volume) {
        switch (shipId) {
            case SHIP_SMALL_CARGO:
                return (volume / VOLUME_SMALL_CARGO) + 1;
            case SHIP_LARGE_CARGO:
                return (volume / VOLUME_LARGE_CARGO) + 1;
            default:
                return 0;
        }
    }

    public static int attackShipNumber(int shipId, int volume) {
        switch (shipId) {
            case SHIP_SMALL_CARGO:
                return volume / VOLUME_SMALL_CARGO / 2;
            case SHIP_LARGE_CARGO:
                return volume / VOLUME_LARGE_CARGO / 2;
            default:
                return 0;
        }
    }

}
